*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   	 Local Data 
*
*===============================================================================


********************************************************************************
*								Figure B03	 								   *
********************************************************************************

clear


* define program for plotting 
capture program drop twbinscatter_basic	
program define twbinscatter_basic

	args yvar xvar ytitle xtitle ylabel xlabel marker_treat marker_control patt_treat patt_control connected_place lab0 lab1
	
reg   `yvar' `xvar' if gsoep==1
	  local cons_m = string(round(_b[_cons],0.001),"%4.3f")
      local beta_m = string(round(_b[`xvar'],0.001),"%4.3f") 
      local se_m = string(round(_se[`xvar'],0.001),"%4.3f") 
	  local samp_m = e(N)

reg   `yvar' `xvar' if gsoep==0
	  local cons_f = string(round(_b[_cons],0.001),"%4.3f")
      local beta_f = string(round(_b[`xvar'],0.001),"%4.3f") 
      local se_f = string(round(_se[`xvar'],0.001),"%4.3f") 
	  local samp_f = e(N)
	  
preserve
qui binscatter `yvar' `xvar' if gsoep==1, savedata("$temp/binned_`yvar'_treated") replace
restore

preserve
qui binscatter `yvar' `xvar' if gsoep==0, savedata("$temp/binned_`yvar'_control") replace
restore

preserve
import delimited "$temp/binned_`yvar'_treated.csv", clear
gen gsoep=1
summ `xvar'
local min_m : di %7.3g `r(min)'
local max_m : di %7.3g `r(max)'
tempfile treated
save `treated'

import delimited "$temp/binned_`yvar'_control.csv", clear

gen gsoep=0
summ `xvar'
local min_f : di %7.3g `r(min)'
local max_f : di %7.3g `r(max)'
tempfile control
save `control'

use `treated', clear
append using `control'

twoway (connect `yvar' `xvar' if `yvar'==`connected_place', lc(navy) mc(navy) msymbol(O) lpattern(solid)) /// 
(connect `yvar' `xvar' if `xvar'==`connected_place', lc(maroon) mc(maroon) msymbol(Th) lpattern(dash)) /// 
(function 0*x^2+`beta_m'*x+`cons_m', range(`min_m' `max_m') lp(`patt_treat') lc(navy)) ///
(scatter `yvar' `xvar' if gsoep == 1 , mc(navy) m(`marker_treat')) ///
(function 0*x^2+`beta_f'*x+`cons_f', range(`min_f' `max_f') lp(`patt_control') lc(maroon)) ///
(scatter `yvar' `xvar' if gsoep == 0 , mc(maroon) m(`marker_control')) , ///
ytitle("`ytitle'") xtitle("`xtitle'") xlabel(`xlabel', grid)  ylabel(`ylabel', grid)  legend(lab(1 "`lab0' `beta_m' (SE `se_m')") lab(2 "`lab1' `beta_f' (SE `se_f')") size(medsmall) rows(2) region(c(none)) symysize(5) margin(vsmall) bmargin(6) order(1 2) pos(10) ring(0)) 		
	

erase "$temp/binned_`yvar'_treated.csv"
erase "$temp/binned_`yvar'_treated.do"
erase "$temp/binned_`yvar'_control.csv"
erase "$temp/binned_`yvar'_control.do"

end
	
	
*** Load data

* SOEP
use "$temp/GSOEPIS_survey.dta", clear
* outside option	
gen outsideo_pre = - monetary_surplus_pct_w219 / 100 
gen outsideo_post = - monetary_surplus_pct_w220	/ 100
* further sample specifications
keep if samejob20 == 1 // only obs who keep the same job 

* Persistence of Beliefs about Outside Option
keep outsideo_pre outsideo_post 	

* SOEP Data 
save "$temp/gsoep.dta", replace

*Info Exp
use "$temp/experiment1.dta", replace

* adjust variables
drop if personalinfo == 1 // only keep control group	
drop if prespecified == 0 // only pre-specified sample
rename pretreatment_wagechange_pct_w2 outsideo_pre
rename posttreatment_wagechange_pct_w2 outsideo_post
keep outsideo_pre outsideo_post

* combine data sets
append using "$temp/gsoep.dta", gen(gsoep)

twbinscatter_basic outsideo_post outsideo_pre "t=2 Belief About Wage Change if Moving to Outside Option" "t=1 Belief About Wage Change if Moving to Outside Option" #5 #5 O Th line dash 1 "Medium-Term Belief:" "Short-Term Belief:"
graph export "$fig/FigureB03.pdf", replace
	